package com.example.TeacherComponent;

import com.example.AdminComponent.UpdateJFrameStudent;
import com.example.Utils.HttpUtil;
import com.example.Utils.ResultUtil;
import com.example.pojo.ParseStudent;
import com.example.pojo.ParseTeacher;

import javax.swing.*;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import java.util.Vector;

public class TeacherComponentStudent extends Box{

    JTable Table;
    Vector<String> student=new Vector<>();
    Vector<Vector<String>> students=new Vector<Vector<String>>();
    TableModel tm;


    //构造方法
    public TeacherComponentStudent(String user) {
        super(BoxLayout.Y_AXIS);
        //组装
        JPanel jPanel = new JPanel();
        jPanel.setBackground(new Color(208, 210, 213, 179));
        jPanel.setMaximumSize(new Dimension(700, 50));
        JButton b1=new JButton("编辑");
        JButton b3=new JButton("查询");
        JLabel name=new JLabel("学号");
        JLabel aclass=new JLabel("姓名");
        JTextField t1=new JTextField(6);
        JTextField t2=new JTextField(6);
        jPanel.add(name);
        jPanel.add(t1);
        jPanel.add(aclass);
        jPanel.add(t2);
        jPanel.add(b3);
        jPanel.add(Box.createHorizontalStrut(50));
        jPanel.add(b1);
        this.add(jPanel);

        //组装表格
        String[] ss = {"学号", "姓名", "班级", "课程", "成绩"};
        for (int i = 0; i < ss.length; i++) {
            student.add(ss[i]);
        }
        //根据id查出自己的信息
        Vector<ParseTeacher> parseTeachers = requestGetDataTeacher("http://localhost:8080/selectTeacher?id=" + user);
            String teacher=parseTeachers.get(0).getName();
            //查出选择自己课程的学生
            requestGetDataStudent("http://localhost:8080/select?teacher="+teacher);


        tm = new DefaultTableModel(students, student);
        Table = new JTable(tm) {
            @Override
            public boolean isCellEditable(int row, int column) {
                return false;
            }
        };

        //只能选中一行
        Table.setSelectionMode(ListSelectionModel.SINGLE_INTERVAL_SELECTION);
        DefaultTableCellRenderer tcr = new DefaultTableCellRenderer();//单元格渲染器
        tcr.setHorizontalAlignment(JLabel.CENTER);//居中显示
        Table.setDefaultRenderer(Object.class, tcr);//设置渲染器
        JScrollPane scrollPane = new JScrollPane(Table);
        Table.setFont(new Font("宋体", Font.PLAIN, 15));
        this.add(scrollPane);





        //查询按钮绑定
        b3.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                if (t2.getText().trim().equals("")&&!t1.getText().trim().equals("")) {
                    requestGetDataStudent("http://localhost:8080/select" + "?id=" + t1.getText()+"&teacher="+teacher);
                }else
                if (t1.getText().trim().equals("")&&!t2.getText().trim().equals("")) {
                    requestGetDataStudent("http://localhost:8080/select" + "?name=" + t2.getText()+"&teacher="+teacher);
                } else  if (!t1.getText().trim().equals("")&&!t2.getText().trim().equals("")){
                    requestGetDataStudent("http://localhost:8080/select" + "?id=" + t1.getText()+"&teacher="+teacher + "&name=" + t2.getText());
                }else {
                   requestGetDataStudent("http://localhost:8080/select"+"?teacher="+teacher);
                }
                //刷新表格
                Table.updateUI();
            }
        });
       //编辑按钮
        b1.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                int row = Table.getSelectedRow();//获取行
                //   DefaultTableModel dm= (DefaultTableModel) Table.getModel();//获取内容模型
                //弹出面板
                JFrame update = new UpdateScoreJFrame((String) tm.getValueAt(row, 0), (String) tm.getValueAt(row, 1),
                        (String) tm.getValueAt(row, 2), (String) tm.getValueAt(row, 3), (String) tm.getValueAt(row, 4),Table);
            }
        });
    }










    public  void requestGetDataStudent( String url) {
        String result = HttpUtil.sendGet(url);
        Vector<ParseStudent> parseStudents = ResultUtil.ResultVectorStudent(result);
        Vector<Vector<String>> ss = new Vector<>();
        for (int i = 0; i < parseStudents.size(); i++) {
            Vector<String> s = new Vector<>();
            s.add(parseStudents.get(i).getId());
            s.add(parseStudents.get(i).getName());
            s.add(parseStudents.get(i).getAaClass());
            s.add(parseStudents.get(i).getCourse());
            s.add(String.valueOf(parseStudents.get(i).getScore()));
            ss.add(s);
        }
       students.clear();
        for (Vector<String> sv  : ss) {
            students.add(sv);
        }

    }

    public Vector<ParseTeacher> requestGetDataTeacher(String url) {
        String result = HttpUtil.sendGet(url);
        Vector<ParseTeacher> parseTeachers = ResultUtil.ResultVectorTeacher(result);
        return parseTeachers;
    }


}

